%m1 = [2.79 2.44 1 1 1 1 1 1 1 1 1 1];
%m2 = [-0.28 4.54 1 1 1 1 1 1 1 1 1 1];
%m3 = [m1 ; m2];
clear;
%mix = [2 4 8 16];
mix = [4];
%iter = [2 3 4 5 6 7 8 9 10];
iter = [8];
%iter = [2]

%all_classes = {'phonemes/nasals.dat.txt'};
%all_classes = {'phonemes/stops.dat.txt'};
%all_classes  = {'phonemes/affricates.dat.txt','phonemes/fricatives.dat.txt','phonemes/vowels.dat.txt'};



phoneme_class(1).name = 'a';
phoneme_class(1).list = textread(char('phonemes/affricates.dat.txt'),'%s');
phoneme_class(1).weight_raw = 0.0045 ;
phoneme_class(1).weight_subsampled = 0.0828;
phoneme_class(1).weight_subsampled_norm = 0.0092;
phoneme_class(1).weight_nil = 0;

phoneme_class(2).name = 'f'
phoneme_class(2).list = textread(char('phonemes/fricatives.dat.txt'),'%s');
phoneme_class(2).weight_raw = 0.0606 ;
phoneme_class(2).weight_subsampled = 0.1223 ;
phoneme_class(2).weight_subsampled_norm = 0.0658;
phoneme_class(2).weight_nil = 0;

phoneme_class(3).name = 'L'
phoneme_class(3).list = textread(char('phonemes/liquids_glides.dat.txt'),'%s');
phoneme_class(3).weight_raw = 0.1590 ;
phoneme_class(3).weight_subsampled = 0.1739;
phoneme_class(3).weight_subsampled_norm = 0.1807;
phoneme_class(3).weight_nil = 0;

phoneme_class(4).name = 'n'
phoneme_class(4).list = textread(char( 'phonemes/nasals.dat.txt'),'%s');
phoneme_class(4).weight_raw = 0.0379 ;
phoneme_class(4).weight_subsampled = 0.2071;
phoneme_class(4).weight_subsampled_norm = 0.3189;
phoneme_class(4).weight_nil = 0;

phoneme_class(5).name ='v'
phoneme_class(5).list = textread(char('phonemes/vowels.dat.txt'),'%s');
phoneme_class(5).weight_raw = 0.4864 ;
phoneme_class(5).weight_subsampled = 0.3195;
phoneme_class(5).weight_subsampled_norm = 0.3856 ;
phoneme_class(5).weight_nil = 0;

phoneme_class(6).name  ='s'
phoneme_class(6).list = textread(char('phonemes/stops.dat.txt'),'%s');
phoneme_class(6).weight_raw = 0.2516 ;
phoneme_class(6).weight_subsampled = 0.0943  ;
phoneme_class(6).weight_subsampled_norm = 0.0398;
phoneme_class(6).weight_nil = 0;





    %frames_path = char(strcat('mfcc_per_phone_class/frames_',strrep(path,'phonemes/','')))
    %file_2 = fopen(frames_path,'w');
for m = 1:size(mix,2)
    for it = 1:size(iter,2) 
        gmm_mixtures =mix(m);
        iterations = iter(it);

        initial_means = ones(gmm_mixtures,12);
        %initial_means = (rand(4,12)  - 0.5) .* 4;
        initial_variance = ones(gmm_mixtures,12);
        initial_weights = ones(gmm_mixtures,1) * (1/gmm_mixtures);

        user(1000).name = '';
        user(1000).mean = [];
        user(1000).variance = [];
        user(1000).weights = [];


        test_user(1000).name = '';
        test_user(1000).file_name = cell(1,100);
        test_user(1000).test_vectors = cell(1,100);
        test_user(1000).phone_list  = cell(1,100);
        test_user(1000).start_times =  cell(1,100);
        test_user(1000).end_times = cell(1,100);

        folders = pathlist(genpath('TIMIT/TRAIN/'));
        count_users = 0;

        cumulative_phn_class_mfcc = 0;
        cumulative_mfcc = 0;

        for f = 1:size(folders,1)
           folder = folders(f);
           folder = folder
           wavs = dir(strcat(char(folder),'/*.WAV'));

           if (size(wavs,1) > 0)
            folder = folder;
            count_users = count_users + 1;
            user(count_users).name = char(folder);
            test_user(count_users).name = char(folder);

            audio_data = [];
            st_data = [];
            en_data=[];
            phoneme_data = [];
            Fs = [];

            %first 20 percent of wav files set aside for testing

            set_aside_for_test = size(wavs,1) * 0.2;

            for t_w = 1:uint8(set_aside_for_test)
               t_w = t_w;
               [raw_data_test Fs WRD PHN] =  readsph(strcat(char(folder),'/',wavs(t_w).name));
               disp('testing:');
               wavfile = strcat(char(folder),'/',wavs(t_w).name)
               phnfile = strrep(wavfile,'.WAV','.PHN');
               [st en phonemes_of_test_file] = textread(phnfile,'%d %d %s');
               mfcc_of_test_file = melcepst(raw_data_test,8000);
               %phn_class_index_of_test_file = find(ismember(phonemes_of_test_file,phn_class) == 1);
               %phn_class_mfcc_of_test_file = (SubSample(mfcc_of_test_file,st(phn_class_index_of_test_file) ./ 64,en(phn_class_index_of_test_file) ./ 64))';

               %if( isequal(size(phn_class_mfcc_of_test_file),[0 0]))
               test_user(count_users).file_name{t_w}  = char(wavfile);
               test_user(count_users).test_vectors{t_w} = mfcc_of_test_file;
               test_user(count_users).phone_list{t_w} = phonemes_of_test_file;
               test_user(count_users).start_times{t_w} = st ./ 64;
               test_user(count_users).end_times{t_w} = en ./ 64;

               %else
               %   test_user(count_users).test_vectors{t_w} = phn_class_mfcc_of_test_file';
               %end

            end

            %remaining wav files used for training

            for w = uint8(set_aside_for_test)+1:size(wavs,1)
                w =w;
                [raw_data Fs] =  readsph(strcat(char(folder),'/',wavs(w).name));
                disp('training:');
                wavfile = strcat(char(folder),'/',wavs(w).name)
                phnfile = strrep(wavfile,'.WAV','.PHN');
                [st en phonemes] = textread(phnfile,'%d %d %s');
                st_data = [st_data; st + size(audio_data,1)];
                en_data = [en_data; en + size(audio_data,1)];
                phoneme_data = [phoneme_data; phonemes];
                %mfc = size(mfcc_audio_data)
                %audio = size(audio_data)
                audio_data = [audio_data;  raw_data];
            end
               %sub_sample_audio_data =  
               %size(st_data);
               %size(en_data);
               %size(phoneme_data);
               %phn_class_index = find(ismember(phoneme_data,phn_class) == 1);
               %ae_audio_data = SubSample(audio_data,st_data(ae_index),en_data(ae_index));


               %can create different mfcc data (read in the folders above,
               %outside of all loops) ... list of phn_classs, etc. subsample
               %will be for all the phn_classs, or whatever. wherever ae_index
               %occurs, this will be replaced by whatever you're looking
               %for. If there's more than one phoneme


               %phn_class_mfcc_audio_data = (SubSample(mfcc_audio_data,st_data(phn_class_index) ./ 64,en_data(phn_class_index) ./ 64))';
               %cumulative_phn_class_mfcc = size(phn_class_mfcc_audio_data,2) + cumulative_phn_class_mfcc;
               %cumulative_mfcc = size(mfcc_audio_data,1) + cumulative_mfcc;

               %FUTHER SUBSAMPLING SO THAT ALL PHONE CLASSES USE THE
               %SAME NUMBER OF MFCCS IN TRAINING
               %sub_ratio = lowest_freq/class_freq;
               %s = randsample(1:size(phn_class_mfcc_audio_data,2), floor(size(phn_class_mfcc_audio_data,2) * sub_ratio));
               %phn_class_mfcc_audio_data = phn_class_mfcc_audio_data(:,s);
               %%mfcc = size(mfcc_audio_data)
               %%audio = size(audio_data)
               %%rand_index = uint32(rand(gmm_mixtures,1) * size(mfcc_audio_data,1));
               %%rand_index(rand_index == 0) = [1];

               %rand_index = rand_index
               %%initial_means = mfcc_audio_data(rand_index',:);
               %gaussmix will apply to whatever you're looking for,
               %ae_mfcc, etc. 


               %rand_index = 1:uint32(size(phn_class_mfcc_audio_data,2)/gmm_mixtures):size(phn_class_mfcc_audio_data,2)
               %rand_index(gmm_mixtures+1:size(rand_index,2)) = []
               %rand_index(rand_index == 0) = [1]

                mfcc_audio_data = melcepst(audio_data,8000);
                rand_index =  randsample(size(mfcc_audio_data,2),int8(gmm_mixtures));%uint32(rand(gmm_mixtures,1) * size(phn_class_mfcc_audio_data,1));
                initial_means = mfcc_audio_data(rand_index',:);   
                [gmm_mu,gmm_sigma,gmm_weights] = gaussmix(mfcc_audio_data,[], iterations,initial_means, initial_variance, initial_weights);
                user(count_users).mean = gmm_mu;
                user(count_users).variance = gmm_sigma;
                user(count_users).weights = gmm_weights;

                %if (isequal ( size(phn_class_mfcc_audio_data),[0 0]))

               %else
                %   size_of_phone_class_mfcc  = size(phn_class_mfcc_audio_data)
                %   rand_index =  randsample(size(phn_class_mfcc_audio_data,2),int8(gmm_mixtures))%uint32(rand(gmm_mixtures,1) * size(phn_class_mfcc_audio_data,1));
                %   
                %   phn_class_mfcc_audio_data = phn_class_mfcc_audio_data';
                %   initial_means = phn_class_mfcc_audio_data(rand_index',:);
                %   size_vmfcc = size(phn_class_mfcc_audio_data);
                %   size_iter = size(iterations);
                %   size_mean = size(initial_means);
                %   size_var = size(initial_variance);
                %   size_weight = size(initial_weights);

               %end

           end

        end

        %fprintf(file_2,'%d\t%d\t%d\t%d\n',cumulative_phn_class_mfcc,cumulative_mfcc, uint8(gmm_mixtures), uint8(iterations));

        user(count_users+1:1000) = [];

        %rand_index(gmm_mixtures+1:size(rand_index,1)) = [];

        user = user

        test_user(count_users+1:1000) = [];
        test_user = test_user


        count_of_total_test_vectors = 0;
        count_of_correct_test_vectors = 0;
        for t = 1:size(test_user,2)

            disp(sprintf(' testing %d of %d ', t, count_users));
            test_vectors = test_user(t).test_vectors;
            for t_v = 1:size(test_user(t).test_vectors,2)
                test_mfcc = test_user(t).test_vectors{t_v};
                test_phone_list = test_user(t).phone_list{t_v};
                test_start_time = test_user(t).start_times{t_v};
                test_end_time = test_user(t).end_times{t_v};
                test_user(t).file_name{t_v}
                
                max_mean_lp_user = [];
                max_mean_lp = -Inf;
                for u = 1:size(user,2)
                    if (isequal(user(u).mean,[]))

                    else
                        [lp rp kh kp] = gaussmixp(test_mfcc,user(u).mean, user(u).variance, user(u).weights);
                       
                        weights_list = ones(size(lp));
                        phones_for_each_mfcc = [];
                        for pc = (1:size(phoneme_class,2))
                            phn_class_index = find(ismember(test_phone_list,phoneme_class(pc).list) == 1);
                            st_phn = test_start_time(phn_class_index);
                            en_phn = test_end_time(phn_class_index);
                            for tr = (1:size(st_phn,1))
                                st_phn(tr);
                                en_phn(tr);
                                weights_list(floor(st_phn(tr)):ceil(en_phn(tr))) = 1 - phoneme_class(pc).weight_subsampled_norm;
                                phoneme_class(pc).name;
                                phones_for_each_mfcc(floor(st_phn(tr)):ceil(en_phn(tr))) = phoneme_class(pc).name;
                                %phones_for_each_mfcc(floor(st_phn(tr)):floor(en_phn(tr))) = phoneme_class(pc).name;
                            end
                        end

                        lps_drop_index = find(not(weights_list));
                        new_lp = lp;
                        new_lp(lps_drop_index) = [];
                        weights_list(lps_drop_index) = [];
                        sum_of_weights = sum(weights_list);
                        
                        new_lp = new_lp .* weights_list;
                        sum_of_new_lp = sum(new_lp);
                        
                        mean_new_lp = sum_of_new_lp/sum_of_weights;
                        
                        %mean_lp = mean (lp);
                        mean_lp = mean (mean_new_lp);
                        if(mean_lp > max_mean_lp)
                            max_mean_lp = mean_lp;
                            max_mean_lp_user = user(u);
                        end
                    end

                end
                count_of_total_test_vectors = count_of_total_test_vectors + 1;
                if( strcmp(test_user(t).name,max_mean_lp_user.name) == 1)
                    count_of_correct_test_vectors = count_of_correct_test_vectors +1;
                end
            end
        end
        count_of_correct_test_vectors = count_of_correct_test_vectors
        count_of_total_test_vectors = count_of_total_test_vectors
        %combo  = combo + 1;
        correct = count_of_correct_test_vectors/count_of_total_test_vectors;
        %accuracy_report{combo} = sprintf(' mixtures = %d iterations = %d accuracy = %f ', gmm_mixtures, iterations,count_of_correct_test_vectors/ count_of_total_test_vectors)
        %fprintf(file_1,'%d\t%d\t%f\n',gmm_mixtures,iterations, correct);
    end
end
beep
beep
%fclose(file_1)













